"ui";
var appName = "QQ群发脚本";
ui.statusBarColor("#FF4FB3FF")//最最上面的顔色，设置当前界面的状态栏颜色

ui.layout(
    <vertical>
        <appbar>
            <toolbar id="toolbar" bg="#ff4fb3ff" title="{{appName}}">
                <button id="exit" bg="#ff4fb3ff" text="退出"
                    layout_gravity="right"
                    textColor="#ffffff"
                    textSize="20"
                    style="Widget.AppCompat.Button.Borderless" />
            </toolbar>
        </appbar>

        <button id="start" text="运行脚本" />
        <card w="*" h="auto" margin="10" cardCornerRadius="6dp"
            cardElevation="2dp" gravity="center">

            <vertical margin="10rpx">
                <text id="peopleList" text="人员名单"></text>
                <input hint="输入内容" id='userin'></input>
            </vertical>
        </card>

        <grid spanCount="3" id='dataList'>
            <checkbox w="auto" id="a1" text='{{this.name}}' marginRight="10"></checkbox>
        </grid>
    </vertical>
)


var dataList = [
    { name: "啊a测试1", qq: 123456},
    { name: "哈h测试2", qq: 123456},
    { name: "喀k测试3", qq: 123456},
    { name: "七q测试4", qq: 123456},
    { name: "一y测试5", qq: 123456},
]

//排序
dataList.sort(function compareFunction(item1, item2) {
    var t1 = item1.name
    var t2 = item2.name
    return t1.localeCompare(t2);
});
var nameList = []

ui.dataList.setDataSource(dataList)
ui.dataList.on("item_click", function (item) {
    //console.log(item)//{ name: '测试2', qq: 672788008 }
    var pos = nameList.indexOf(item.name)
    if (pos != -1) {
        nameList.splice(pos, 1)
    } else {
        nameList.push(item.name)
    }
    ui.peopleList.setText(nameList.toString())

});

ui.exit.click(() => {
    log("退出脚本");
    threads.shutDownAll()
    exit()
});
ui.start.click(function () {
    threads.start(function () {
        console.show();
    });

    //通过getText()获取输入的内容
    var text = ui.userin.getText();
    console.log("通知内容：")
    console.log(text);
    if(text.length() == 0){
        console.error("不能为空")
        return
    }

    console.log("通知人员：")
    console.log(nameList)
    if(nameList.length == 0){
        console.error("不能为空")
        return
    }

    toast("开始通知");

    var 子线程 = threads.start(function () {
        for (i = 0; i < nameList.length; i++) {
            toastLog("===准备通知：" + nameList[i]);
            var qq = dataList.find(items => items.name === nameList[i]).qq
            console.verbose(qq);
            try {
                app.startActivity({
                    action: "android.intent.action.VIEW",
                    data: "mqq://im/chat?chat_type=wpa&version=1&uin=" + qq,
                    packageName: "com.tencent.mobileqq",
                });
            } catch (e) {
                console.error(e)
            }
            sleep(1000)
            while (!className("EditText").depth(10).id("input").findOnce()) {//等待跳转页面成功
                console.verbose("等待跳转");
                sleep(1000)
                app.startActivity({
                    action: "android.intent.action.VIEW",
                    data: "mqq://im/chat?chat_type=wpa&version=1&uin=" + qq,
                    packageName: "com.tencent.mobileqq",
                });
            }
            input(text)//输入文字
            toast("请检查消息，并点击发送点击发送")
            sleep(1000)
            //点击发送
            while (!textStartsWith("发送").findOnce().click());
            toastLog("点击发送")
            //发送成功
            while (!className("TextView").depth(10).id("chat_item_content_layout").textContains(text).findOnce()) {
                console.verbose("等待发送");
                sleep(1000)
            }
            console.log(nameList[i] + " 完成===");
        }
        toast("通知完成");
        console.info("通知完成,脚本结束");
        exit()
    });

});
